# -*- coding: utf-8 -*-
# @Time    : 2024/11/7 下午1:56
# @Author  : xiaowanzi
# @File    : alter_html.py
from base.project_path import get_project_path
from bs4 import BeautifulSoup
from time import sleep
import os


def alter_test_report(filename):
    """
    修改测试报告css 和 js文件地址
    :param filename: 文件名
    """
    file_path = str(get_project_path) + filename
    for _ in range(5):
        if os.path.exists(file_path):
            break
        else:
            sleep(2)
    else:
        print("*****测试报告文件不存在！！！****")
        return None
    url = "http://10.0.98.143:8686/css/"
    with open(file_path, "r", encoding="utf-8") as file:
        fcontent = file.read()
        sp = BeautifulSoup(fcontent, 'html.parser')

        link_list = sp.findAll('link')
        for link in link_list:
            if "href" in link.attrs.keys():
                if "cdn.bootcss.com" in link['href']:
                    href = url + link['href'].split("/")[-1]
                    link['href'] = href

        script_list = sp.findAll('script')
        for script in script_list:
            if "src" in script.attrs.keys():
                if "cdn.bootcss.com" in script['src']:
                    src = url + script['src'].split("/")[-1]
                    script['src'] = src

    with open(file_path, 'w', encoding="utf-8") as fp:
        fp.write(sp.prettify())


if __name__ == "__main__":
    alter_test_report("")
